/// @tags: DP on VirtualTree
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>

using std::cin;
using std::cout;

namespace BlueQuantum {

typedef std::pair<int, int> PII;

constexpr int MAXN = 5e4 + 3, MAXM = 1e5 + 3, MAXNUM = 5e2 + 3, INF = 0x3f3f3f3f;

int n, m, k, q;

template <size_t _MAXN, size_t _MAXM, bool DIR>
struct Graph {
  struct Edge {
    int v, nxt, w;
  };

  int ecnt;
  int heads[_MAXN];
  Edge e[DIR ? _MAXM : _MAXM * 2];

  inline int& operator()(int u) { return heads[u]; }
  inline Edge& operator[](int index) { return e[index]; }

  inline void add(int u, int v, int w) {
    e[++ecnt].v = v, e[ecnt].nxt = heads[u], heads[u] = ecnt, e[ecnt].w = w;
  }
};

Graph<MAXN, MAXM, false> G;
Graph<MAXN, MAXN, true> T;
Graph<MAXN, MAXNUM * 2, true> VT;

inline void predij() {
  static int dis[MAXN], pre[MAXN], len[MAXN];
  std::priority_queue<PII, std::vector<PII>, std::greater<PII> > pq;
  memset(dis, 0x3f, sizeof(dis));
  pq.emplace(dis[k] = 0, k);
  while (!pq.empty()) {
    int u = pq.top().second, d = pq.top().first;
    pq.pop();
    if (d != dis[u]) continue;
    for (int i = G(u); i; i = G[i].nxt) {
      int v = G[i].v;
      if (dis[v] > dis[u] + G[i].w) {
        dis[v] = dis[u] + G[i].w;
        pre[v] = u;
        len[v] = G[i].w;
        pq.emplace(dis[v], v);
      } else if (dis[v] == dis[u] + G[i].w && pre[v] > u) {
        pre[v] = u;
        len[v] = G[i].w;
      }
    }
  }
  for (int i = 1; i <= n; ++i)
    if (i != k) {
      T.add(pre[i], i, len[i]);
    }
}

namespace TCP {

int dcnt;
int fa[MAXN], dep[MAXN], dfn[MAXN], dis[MAXN], son[MAXN], top[MAXN], siz[MAXN];

void dfs1(int u) {
  siz[u] = 1;
  for (int i = T(u); i; i = T[i].nxt) {
    int v = T[i].v;
    fa[v] = u;
    dep[v] = dep[u] + 1;
    dis[v] = dis[u] + T[i].w;
    dfs1(v);
    siz[u] += siz[v];
    if (siz[v] > siz[son[u]]) son[u] = v;
  }
}

void dfs2(int u) {
  dfn[u] = ++dcnt;
  if (son[u]) {
    top[son[u]] = top[u];
    dfs2(son[u]);
  }
  for (int i = T(u); i; i = T[i].nxt) {
    int v = T[i].v;
    if (v != son[u]) {
      top[v] = v;
      dfs2(v);
    }
  }
}

inline int getLCA(int x, int y) {
  while (top[x] != top[y]) {
    if (dep[top[x]] < dep[top[y]]) std::swap(x, y);
    x = fa[top[x]];
  }
  return dep[x] < dep[y] ? x : y;
}

inline void init() {
  dfs1(k);
  top[k] = k, dfs2(k);
}

}  // namespace TCP
using TCP::dfn;
using TCP::dis;
using TCP::getLCA;

int top;
int stk[MAXN], put[MAXN], key[MAXNUM];

int solve(int u) {
  int res = 0;
  for (int i = VT(u); i; i = VT[i].nxt) {
    int v = VT[i].v;
    if (put[v]) {
      res += VT[i].w;
    } else {
      res += std::min(VT[i].w, solve(v));
    }
  }
  return res;
}

/// @brief dep[u] < dep[y]
inline void link(int u, int v) { VT.add(u, v, dis[v] - dis[u]); }

inline int main() {
  cin >> n >> m >> k >> q;
  for (int i = 1; i <= m; ++i) {
    int u, v, w;
    cin >> u >> v >> w;
    G.add(u, v, w), G.add(v, u, w);
  }
  predij();
  TCP::init();
  while (q--) {
    int opt, num;
    cin >> opt >> num;
    switch (opt) {
      case 0:
        for (int i = 1; i <= num; ++i) {
          int x;
          cin >> x;
          put[x] ^= 1;
        }
        break;
      case 1:
        for (int i = 1; i <= num; ++i) cin >> key[i];
        key[0] = k;
        std::sort(key, key + num + 1, [&](int a, int b) { return dfn[a] < dfn[b]; });
        stk[top = 1] = k;
        VT(k) = 0;
        VT.ecnt = 0;
        for (int i = 1; i <= num; ++i) {
          int lca = getLCA(key[i], stk[top]);
          if (lca != stk[top]) {
            while (dfn[stk[top - 1]] > dfn[lca]) {
              link(stk[top - 1], stk[top]);
              top--;
            }
            if (lca != stk[top - 1]) {
              VT(lca) = 0;
              link(lca, stk[top]);
              stk[top] = lca;
            } else {
              link(lca, stk[top--]);
            }
          }
          VT(key[i]) = 0, stk[++top] = key[i];
        }
        while (top > 1) link(stk[top - 1], stk[top]), top--;
        int ans = solve(k);
        cout << (ans == 0 ? -1 : ans) << '\n';
        break;
    }
  }
  return 0;
}

}  // namespace BlueQuantum

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("/tmp/CodeTmp/testdata.in", "r", stdin);
  freopen("/tmp/CodeTmp/testdata.out", "w", stdout);
#else
  freopen("P5680 [GZOI2017]共享单车.in", "r", stdin);
  freopen("P5680 [GZOI2017]共享单车.out", "w", stdout);
#endif
#endif

  std::ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  return BlueQuantum::main();
}
